package com.wxx.controller.upload;

/**
 * @Auther: 陈远明
 * @Date: 2019/4/22 22 : 16
 * @Description:
 */

import com.wxx.service.tool.oss.OssService;
import com.wxx.util.PropertiesUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/ued")
public class UEditorController {

    @Resource
    private OssService ossService;

    @Resource
    private UploadController uploadController;


    @RequestMapping("/serverUrl")
    @ResponseBody
    public Object serverUrl(HttpServletRequest request,
                       @RequestParam(value = "action") String action,
                       @RequestParam(value = "upfile", required = false) MultipartFile file) throws Exception {
        switch (action) {
            case "config": // 加载返回ueditor配置文件conf/config.json
                return getConfig();
            case "uploadimage": // 上传文件
                return uploadController.uploadFileByUEditor(request,file,1);
            case "uploadvideo": // 上传视频
                return "视频处理方法";
            case "uploadfile": // 上传文件
                return uploadController.uploadFileByUEditor(request,file,1);
            default:
                return "无效action";
        }
    }


    private String getConfig() throws Exception {
        //ResourceUtils.getFile("classpath:config.json") ,这个方法只能从类路径下获取文件,
        // 无法从jar包中获取,所以打成jar包后发布到linuxs系统,就报错找不到文件了
//        File file = ResourceUtils.getFile("classpath:config.json");
//        String json = FileUtils.readFileToString(file, "utf-8");

        //打成 jar包部署 需要这样获取文件，否则会出现
//        class path resource [config.json] cannot be resolved to absolute file path because it does not resid
        ClassPathResource classPathResource = new ClassPathResource("config.json");

        StringBuilder sb = new StringBuilder();
        try {
            InputStreamReader reader = new InputStreamReader(classPathResource.getInputStream(), "utf-8");
            BufferedReader bufferedReader = new BufferedReader(reader);

            String str = null;
            while ((str = bufferedReader.readLine())!=null){
                sb.append(str);
            }
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    private Map<String, Object> uploadImage(HttpServletRequest request, MultipartFile file) throws IOException {

        Map map = new HashMap();


        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            Map upload = ossService.upload(file);
            map.put("state", "SUCCESS");//返回状态
            Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10);
            map.put("url", PropertiesUtil.getProperty("URL") + upload.get("url"));//这里是设置返回给UEditor的图片地址
            System.out.println(PropertiesUtil.getProperty("URL") + upload.get("url"));
            //一下注释非必须
         /*   map.put("title", file.getOriginalFilename() + file.getContentType());//标题
            map.put("original", file.getOriginalFilename() + file.getContentType());//文件的原始名字*/
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    private Map<String, Object> resultMap(MultipartFile file, String state, String uploadHttpUrl) {
        Map<String, Object> resMap = new HashMap<String, Object>();
        resMap.put("state", state);  //"SUCCESS" 表示成功
        resMap.put("title", file.getOriginalFilename());
        resMap.put("original", file.getOriginalFilename());
        resMap.put("type", file.getContentType());
        resMap.put("size", file.getSize());
        resMap.put("url", uploadHttpUrl);
        return resMap;
    }

}

